package cn.sdormitory.service.impl;

import cn.sdormitory.basedata.dao.BDormitoryDao;
import cn.sdormitory.basedata.dao.BStudentDao;
import cn.sdormitory.basedata.entity.BDormitory;
import cn.sdormitory.basedata.vo.BDormitoryVO;
import cn.sdormitory.service.AppReportService;
import cn.sdormitory.smartdor.dao.SdAttenceDao;
import cn.sdormitory.smartdor.entity.SdAttence;
import cn.sdormitory.smartdor.vo.SdAttenceReportVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author: lx
 * @date: 2023/11/17 10:07
 * @description:
 */
@Service
public class AppReportServiceImpl implements AppReportService {

    @Resource
    BDormitoryDao bDormitoryDao;

    @Resource
    SdAttenceDao sdAttenceDao;

    @Override
    public List<Map<String, Object>> getSdAttendanceRateByBuildStorey(String date) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Date monday = null;
        try {
            monday = format.parse(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }

        List<Map<String, Object>> returnDate = new ArrayList<>();

        //查询正在使用的宿舍楼栋
        List<BDormitoryVO> buildingNoList = bDormitoryDao.getBDormitoryBuildingNo();

        for (int i = 0; i < buildingNoList.size(); i++) {
            Map<String, Object> map1 = new HashMap<>();
            map1.put("buildingNo",buildingNoList.get(i).getBuildingName());
            //查询当前宿舍正在使用的楼层
            List<BDormitoryVO> storeyList = bDormitoryDao.getBDormitoryStoreyByBuildingNo(buildingNoList.get(i).getBuildingNo());

            //每一栋的数据
            LinkedList<Map<String, Object>> buildingNoData = new LinkedList<>();

            for (int j = 0; j < storeyList.size(); j++) {
                //楼栋list数据集合里面的map
                Map<String, Object> map2 = new HashMap<>();
                map2.put("name",storeyList.get(j).getStoreyName());

                //map中的list
                LinkedList<Integer> attendanceRate = new LinkedList<>();

//                int shouldAttendance = bStudentDao.getDormitoryBuildingStoreyStuNum(buildingNoList.get(i).getBuildingNo(),
//                        storeyList.get(j).getStorey());
                int shouldAttendance = sdAttenceDao.getAttenceCountByBuildingStoreyAndDate(buildingNoList.get(i).getBuildingNo(),
                        storeyList.get(j).getStorey(),date,null);
                int actualAttendance = sdAttenceDao.getAttenceCountByBuildingStoreyAndDate(buildingNoList.get(i).getBuildingNo(),
                        storeyList.get(j).getStorey(),date,"1");

                //插入周一的数据
                double result = (double)actualAttendance/shouldAttendance * 100;
                attendanceRate.add((int) result);

                Calendar calendar = Calendar.getInstance();
                calendar.setTime(monday);
                //循环插入周二至周日数据
                for (int k = 0; k < 6; k++) {
                    calendar.add(Calendar.DATE,1);
                    Date nextDate = calendar.getTime();
                    String nextDateString = format.format(nextDate);
                    int actualAttendance2 = sdAttenceDao.getAttenceCountByBuildingStoreyAndDate(buildingNoList.get(i).getBuildingNo(),
                            storeyList.get(j).getStorey(),nextDateString,"1");
                    double result2 = (double)actualAttendance2/shouldAttendance * 100;
                    attendanceRate.add((int) result2);
                }
                map2.put("data",attendanceRate);
                buildingNoData.add(map2);
            }
            map1.put("buildingNoData",buildingNoData);
            returnDate.add(map1);
        }
        return returnDate;
    }

    @Override
    public List<Map<String, Object>> getSdAttenceStuCountByBuildStorey(String date) {

        List<Map<String, Object>> returnDate = new ArrayList<>();

        //查询正在使用的宿舍楼栋
        List<BDormitoryVO> buildingNoList = bDormitoryDao.getBDormitoryBuildingNo();

        for (int i = 0; i < buildingNoList.size(); i++) {
            //楼栋范围内数据
            Map<String, Object> map1 = new HashMap<>();
            map1.put("buildingNo", buildingNoList.get(i).getBuildingName());
            //查询当前宿舍正在使用的楼层
            List<BDormitoryVO> storeyList = bDormitoryDao.getBDormitoryStoreyByBuildingNo(buildingNoList.get(i).getBuildingNo());
            //每一栋的数据
            LinkedList<Map<String, Object>> buildingNoData = new LinkedList<>();

            for (int j = 0; j < storeyList.size(); j++) {
                //每栋楼的楼层范围内数据
                Map<String, Object> map2 = new LinkedHashMap<>();
                map2.put("storey",storeyList.get(j).getStoreyName());

                //map中的list
//                LinkedList<Map<String,Integer>> buildingNoStoreyData = new LinkedList<>();

                List<SdAttence> sdAttenceList = sdAttenceDao.getStuByBuildStorey(buildingNoList.get(i).getBuildingNo(),
                        storeyList.get(j).getStorey(),date,null);

                int inBedCount = 0;
                int violationCount = 0;
                int leaveCount = 0;
                int absence = 0;
                int notClockedIn = 0;
                for(SdAttence item : sdAttenceList){
                    if("1".equals(item.getAttenceStatus())){
                        inBedCount ++;
                    }
                    if("2".equals(item.getAttenceStatus())){
                        notClockedIn ++;
                    }
                    if("4".equals(item.getAttenceStatus())){
                        leaveCount ++;
                    }
                    if("5".equals(item.getAttenceStatus())){
                        violationCount ++;
                    }
                    if("6".equals(item.getAttenceStatus())){
                        absence ++;
                    }
                }
                //楼层的具体数据内容
                Map<String,Integer> map3 = new LinkedHashMap<>();
                map3.put("总人数",sdAttenceList.size());
                map3.put("在寝",inBedCount + violationCount);
                map3.put("违纪（在寝）",violationCount);
                map3.put("请假",leaveCount);
                map3.put("缺勤",absence);
                map3.put("未打卡",notClockedIn);

                map2.put("data",map3);

                buildingNoData.add(map2);
            }
            map1.put("buildingNoData",buildingNoData);
            returnDate.add(map1);
        }
        return returnDate;
    }

    @Override
    public List<SdAttenceReportVo> getStuExcAttence(String startDate,String endDate, String attenceStauts,String buildingNo) {
        return sdAttenceDao.getStuExcAttence(startDate,endDate,attenceStauts,buildingNo);
    }
}
